/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: SysLogAspect
 * Author:   pc
 * Date:     2019/9/9 15:42
 * Description: 日志切面
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.example.rabbitmqdemo.aspect;
import com.alibaba.fastjson.JSON;
import com.example.rabbitmqdemo.annotation.MyLog;
import com.example.rabbitmqdemo.entity.SysLog;
import com.example.rabbitmqdemo.service.SysLogService;
import com.example.rabbitmqdemo.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


/**
 * 〈功能：〉<br> 
 * 〈系统日志：切面处理类〉
 * @author liuhaoran
 * @create 2019/9/9 15:42
 * @since 1.0.0
 */
@Aspect
@Component
@Slf4j
public class SysLogAspect {
	@Autowired
	private SysLogService sysLogService;

	//定义切点 @Pointcut
	//在注解的位置切入代码
	@Pointcut("@annotation(com.example.rabbitmqdemo.annotation.MyLog)")
	public void logPoinCut() {
	}

	//切面 配置通知
	@AfterReturning("logPoinCut()")
	public void saveSysLog(JoinPoint joinPoint) throws ParseException {
		System.out.println("切面。。。。。");
		//保存日志
		SysLog sysLog = new SysLog();

		//从切面织入点处通过反射机制获取织入点处的方法
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		//获取切入点所在的方法
		Method method = signature.getMethod();
		//获取操作
		MyLog myLog = method.getAnnotation(MyLog.class);
		if (myLog != null) {
			String value = myLog.value();
			sysLog.setOperation(value);//保存获取的操作
		}
		//获取请求的类名
		String className = joinPoint.getTarget().getClass().getName();
		//获取请求的方法名
		String methodName = method.getName();
		sysLog.setMethod(className + "." + methodName);
		//请求的参数
		Object[] args = joinPoint.getArgs();
		//将参数所在的数组转换成json
		String params = JSON.toJSONString(args);
		sysLog.setParams(params);
		Date date = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		System.out.println(sdf.format(date ));
		sysLog.setCreateDate(date);
		//获取用户名
		//ShiroUtils.getUserEntity().getUsername()
//		Userinfo Userinfo=new Userinfo();
//		Userinfo.setUserinfoName(ShiroUtils.getUserEntity().getUserinfoName());
		Object object=SecurityUtils.getSubject().getPrincipal();
		//log.info("object",SecurityUtils.getSubject().getPrincipal());
		sysLog.setUsername(ShiroUtils.getUserName());
		//获取用户ip地址
		//HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		sysLog.setIp("127.0.0.1");//IPUtils.getIpAddr(request)
		//调用service保存SysLog实体类到数据库
		sysLogService.savaLog(sysLog);
	}
}